#!/bin/bash
echo "🔍 比较 cbp2make 和 cbp2make_ai 的文件差异..."

# 1. 比较文件列表差异
echo "📁 文件列表差异:"
echo "=== cbp2make_ai 有但 cbp2make 没有的文件 ==="
find . -name "*.cpp" -o -name "*.h" | sed 's|^\./||' | sort > /tmp/ai_files.txt
find ../cbp2make -name "*.cpp" -o -name "*.h" | sed 's|^\.\./cbp2make/||' | sort > /tmp/base_files.txt
comm -23 /tmp/ai_files.txt /tmp/base_files.txt

echo ""
echo "=== 两个目录都有的文件 ==="
comm -12 /tmp/ai_files.txt /tmp/base_files.txt

# 2. 比较文件内容差异
echo ""
echo "📝 文件内容差异:"
for file in $(comm -12 /tmp/ai_files.txt /tmp/base_files.txt); do
    if [ -f "../cbp2make/$file" ] && [ -f "./$file" ]; then
        diff -q "../cbp2make/$file" "./$file" > /dev/null
        if [ $? -ne 0 ]; then
            echo "❌ 内容不同: $file"
            # 显示差异行数
            diff -u "../cbp2make/$file" "./$file" | grep -E "^[-+][^-+]" | head -5
        fi
    fi
done

# 3. 检查 cbp2make.cpp 是否有AI相关修改
echo ""
echo "🤖 检查 cbp2make.cpp 的AI相关修改:"
if [ -f "../cbp2make/src/cbp2make.cpp" ] && [ -f "src/cbp2make.cpp" ]; then
    echo "=== cbp2make_ai 版本的AI相关代码 ==="
    grep -n -A3 -B3 "ai" src/cbp2make.cpp -i | head -20
else
    echo "ℹ️  cbp2make_ai 没有自己的 cbp2make.cpp，使用共享版本"
fi

# 4. 检查头文件差异
echo ""
echo "📋 头文件差异检查:"
for file in $(find . -name "*.h" | sed 's|^\./||'); do
    if [ -f "../cbp2make/$file" ] && [ -f "./$file" ]; then
        diff -q "../cbp2make/$file" "./$file" > /dev/null
        if [ $? -ne 0 ]; then
            echo "❌ 头文件不同: $file"
        fi
    elif [ -f "./$file" ] && [ ! -f "../cbp2make/$file" ]; then
        echo "✅ 新增头文件: $file"
    fi
done

# 清理临时文件
rm -f /tmp/ai_files.txt /tmp/base_files.txt
